var app = getApp();
const shopService = require('../../services/shopService');
const Util = require('../../utils/util');
const moneyCal = require('../../utils/moneyCal');
Component({
    properties: {
        detailData: {
          type: Object,     // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
          value: false     // 属性初始值（可选），如果未指定则会根据类型选择一个
        },
    },
    data:{
        assets:app.assets,
        assetsByEnv:app.assetsByEnv,
        adjustnum:null,
        countDown:0
    },
    ready:function(){
        var adjustnum = moneyCal.sub(this.data.detailData.pay_total,this.data.detailData.real_pay_total);
        this.setData({
            adjustnum:adjustnum
        })

    },
    methods: {
       pay(e) {
        wx.showLoading({
            title:'付款中..',
            mask:true
        });
        var that = this;
        var user = wx.getStorageSync('user');
        var oid = e.currentTarget.dataset.id;
        shopService.applygetPayParams({order_return_id: oid, openid: user.openid}).then((data) => {
            wx.hideLoading();
            if (data.code == 1) {
                var payment = data.data.miniPayRequest;
                if (data.data.channel_type == 3) {
                    payment = data.data.bank_request;
                }
                payment.success = function () {
                    wx.showToast({title: '提交成功~', icon: 'none'});
                    //刷新列表
                    if(that._getPlatform() != 'android') {
                        that._resetList(that.data.status);
                    }
                };
                payment.fail = function () {
                };
                payment.complete = function () {
                };
                wx.requestPayment(payment);
            } else {
                wx.showToast({title: data.msg, icon: 'none'});
            }
        }, (error => {
            wx.hideLoading();
            wx.showToast({title: '获取支付信息500', icon: 'none'});
        }));

      },
      goDeviceDetection(e){
        var id = e.currentTarget.dataset.id;
        var that = this;
        var total_micro_second = 5;
        var user = wx.getStorageSync('user');
        shopService.returnOrderDetectionSend({order_return_id: id, openid: user.openid}).then((data) => {
            if (data.code == 1) {
                    count_down(that, total_micro_second);
            } else {
                wx.showToast({title: data.msg, icon: 'none'});
            }
        }, (error => {
            wx.showToast({title: '获取支付信息500', icon: 'none'});
        }));
      },
      wait(){
        wx.showToast({title: '设备出票过于频繁，请'+this.data.countDown+'秒后重试', icon: 'none'});
      }
    }
});
function count_down(that, total_micro_second) {
    if (total_micro_second <= 0) {
        that.setData({
            countDown: 0,
        });
        return;
    }
    // 渲染倒计时时钟
    that.setData({
        countDown: total_micro_second,
    });
    setTimeout(function () {
        // 放在最后--
        total_micro_second -= 1;
        count_down(that, total_micro_second);
    }, 1000)
}
